Difference Between Function Declarations and Function Expressions

In JavaScript, functions can be defined using Function Declarations or Function Expressions. While they serve similar purposes, they differ in terms of syntax, behavior, and hoisting.

Function Declarations

Example:

        
function greet() {
    console.log("Hello from Function Declaration!");
}

greet(); // Output: Hello from Function Declaration!
        
    

Output: Hello from Function Declaration!

Function Expressions

Example:

        
const greet = function() {
    console.log("Hello from Function Expression!");
};

greet(); // Output: Hello from Function Expression!
        
    

Output: Hello from Function Expression!

Key Differences

Aspect Function Declaration Function Expression
Syntax function name() { ... } const name = function() { ... }
Hoisting Can be called before its definition. Cannot be called before its definition.
Use Case Used for named reusable functions. Used for anonymous functions or assigning functions to variables.